﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Media;
using Tomahawk.Runtime.Resources;

namespace Tomahawk.Runtime.Canvas.Sprite
{
    /// <summary>
    /// An UIVideoSprite allows to play video files
    /// </summary>
    public class UIVideoSprite : Sprite
    {
        /// <summary>
        /// Holds the video asset path
        /// </summary>
        private string videoAsset;

        /// <summary>
        /// If true, the video will be played automatically
        /// </summary>
        private bool autoplay = true;
        
        /// <summary>
        /// If true, the video will be played with no sound
        /// </summary>
        private bool mute = false;

        /// <summary>
        /// If true, the video will be playing in a loop
        /// </summary>
        private bool loop = true;

        /// <summary>
        /// Constructor
        /// </summary>
        public UIVideoSprite(string videoAsset, string effectChannel, ResourceManager.ResourcePool pool, bool autoplay, bool mute, bool loop)
        {
            this.resourcePool = pool;

            this.videoAsset = videoAsset;
            this.EffectChannel = effectChannel;
            this.autoplay = autoplay;
            this.mute = mute;
            this.loop = loop;

            //configure the video player
            this.videoPlayer = new VideoPlayer();
            this.videoPlayer.IsLooped = loop;
            this.videoPlayer.IsMuted = mute;
        }

        /// <summary>
        /// Video the sprite will be playing
        /// </summary>
        Video video = null;

        /// <summary>
        /// Player used to play the video
        /// </summary>
        VideoPlayer videoPlayer = null;

        /// <summary>
        /// Height of the original video.
        /// </summary>
        public float OriginalHeight
        {
            get { return videoPlayer.Video.Height; }
        }

        /// <summary>
        /// Width of the original video.
        /// </summary>
        public float OriginalWidth
        {
            get { return videoPlayer.Video.Width; }
        }

        /// <summary>
        /// Video resource
        /// </summary>
        public Video Video
        {
            get { return video; }
        }

        /// <summary>
        /// Video player 
        /// </summary>
        public VideoPlayer Player 
        { 
            get { return videoPlayer; } 
        }

        private Vector2 origin = new Vector2(0.0f, 0.0f);
        public Vector2 Origin { get { return origin; } set { origin = value; } }

        private float rotation = 0.0f;
        public float Rotation { get { return rotation; } set { rotation = value; } }

        private Rectangle _screenRectangle = new Rectangle();

        /// <summary>
        /// Draw the primitive, must be called with an already opened and configured SpriteBatch
        /// </summary>
        public override void Render(SpriteBatch spriteBatch)
        {
            if (!this.videoPlayer.IsDisposed && (this.videoPlayer.State != MediaState.Stopped))
            {
                Engine.Instance.CanvasManager.ApplyVirtualCanvas(ref this.DestinationRectangle, ref _screenRectangle);
                spriteBatch.Draw(this.videoPlayer.GetTexture(), _screenRectangle, this.SourceRectangle, this.Color,
                    rotation, origin, SpriteEffects.None, this.Depth);
            }
        }

        /// <summary>
        /// Load the contents
        /// </summary>
        protected override bool OnLoad(GraphicsDevice graphics, ContentManager contentManager)
        {
            //load the texture
            this.video = contentManager.Load<Video>(this.videoAsset);

            //set default source size
            this.SourceRectangle = new Microsoft.Xna.Framework.Rectangle(0, 0, this.Video.Width, this.Video.Height);

            //autoplay the video
            if (this.autoplay)
            {
                this.videoPlayer.Play(this.video);
            }
            return true;
        }

    }

}
